package homework;

import com.sun.net.httpserver.HttpServer;
import homework.handler.RequestHandler;
import homework.handler.highstakes.HighStakeHandler;
import homework.handler.session.SessionHandler;
import homework.handler.stake.StakeHandler;
import homework.manager.SessionManager;
import homework.manager.StakeManager;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executors;

public class HttpServerSetup {
    private static final int PORT = 8001;
    private static List<RequestHandler> requestHandlers;

    public static void main(String[] args) throws IOException {
        requestHandlers = Arrays.asList(
                new SessionHandler(),
                new StakeHandler(),
                new HighStakeHandler()
        );
        HttpServer server = HttpServer.create(new InetSocketAddress(PORT), 0);
        server.createContext("/", exchange -> {
            for (RequestHandler requestHandler : requestHandlers) {
                String[] paths = exchange.getRequestURI().getPath().split("/");
                if(requestHandler.requestMethod().contains(exchange.getRequestMethod())) {
                    Object result = requestHandler.parse(exchange, paths);
                    if(result != null) {
                        requestHandler.handle(exchange,result);
                        break;
                    }
                }
            }
        });
        server.setExecutor(Executors.newCachedThreadPool());
        server.start();
        System.out.println("Server started on port " + PORT);
    }
}
